<?php

class Mypdo{
  private $dns        = 'mysql:host=127.0.0.1;port=3306;dbname=luck_draw';#'mysql:host=rm-wz97q0vkb256d0egy.mysql.rds.aliyuncs.com;port=3306;dbname=luck_draw';
    private $name       = 'root';#webdb
    private $password   = 'root';#Bunor(L3
    private static $pdo;
    
    public function __construct($code = "utf8"){
        self::pdoConnect($this->dns, $this->name, $this->password);
        self::setCode($code);
        return self::$pdo;
    }
    
    private static function pdoConnect($dns, $name, $password){
       self::$pdo = new PDO($dns,$name,$password);

       if (empty(self::$pdo)){
            echo '<pre>';
            echo "链接失败<br/>";
            echo self::$pdo->errorCode(),"<br/>";
            var_dump(self::$pdo->errorInfo()); ;
            return;
        }
    }
    
    private static function setCode($code){
        
        self::$pdo->exec('set names '.$code);
    }
    
    public function PdoQueryOne($statement){
        $res = self::$pdo->query($statement);
        return $res->fetch(PDO::FETCH_ASSOC);
    }
    
    public function PdoQueryAll($statement){
        $res = self::$pdo->query($statement);
        return $res->fetchAll(PDO::FETCH_ASSOC);
    }
    
    public function Insert($insertData, $table){
        $insertDataKey = array_keys($insertData);
        $insertDataVal = array_values($insertData);
        foreach ($insertDataVal as &$val){
            $val = "'".$val."'";
        }
        foreach ($insertDataKey as $key => &$val){
            $val = "{$val}";
        }
        $fileds =  implode(',', $insertDataKey);
        $values =  implode(',', $insertDataVal);
      
        $statement = "insert into {$table}({$fileds}) values({$values})";
       
        return self::$pdo->exec($statement);
    }
    
    public function InsertAll($insertData, $table){
        
        $insertDataKey = [];
        $insertDataVal = "";
        foreach ($insertData as $insertkey => $insertVal){
          
            if (!$insertDataKey){
                $insertDataKey = array_keys($insertVal);
                foreach ($insertDataKey as &$Kval){
                    $Kval = "`{$Kval}`";
                }
            }
            foreach ($insertVal as &$val){
                $val = "'".$val."'";
            }
           
            if ($insertkey == count($insertData)-1){
                $insertDataVal .= '('.implode(',', $insertVal).')';
            }else{
                $insertDataVal .= '('.implode(',', $insertVal).'),';
            }
            
        }
       
        $insertKey = implode(',', $insertDataKey);
       
        $statement = "insert into {$table}({$insertKey}) values {$insertDataVal}";
  
        return self::$pdo->exec($statement);
      
    }

    /**
     * @time 2020/12/11 17:16
     * @param $editData array
     * @param $where string
     * @param $table string
     * @return mixed bool
     */
    public function edit($editData, $where, $table){

        $sql = "update {$table} set ";
        $fileds = [];
        foreach ($editData as $key => $val){
            $fileds[] = "`{$key}` = '{$val}'";
        }
        $sqlFileds =  implode(',', $fileds);
        $sql.=$sqlFileds;
        $sql .= " where {$where}";

        return self::$pdo->exec($sql);
    }
    
    public function editAll($editData, $where, $table){
        $editKey = array_keys($editData);
        $editVal= array_values($editData);
        
        foreach ($editData as &$val){
            $val = "'".$val."'";
        }
       
        $statement = "update {$table} set ";
        $count=0;
        foreach ($editData as $key => $newval){
           
            if (count($editData)-1 == $count){
                $statement.="$key=$newval";
            }else{
                $statement.="$key=$newval,";
            }
            $count++;
        }
        $statement .= ' '.$where;
        return self::$pdo->exec($statement);
    }
    
    public function Delete($statement){
        return self::$pdo->exec($statement);
    }
    
    public function Exec($statement){
        return self::$pdo->exec($statement);
    }
    
    public function MakeInsertSql($insertData, $table){
        
        $insertDataKey = [];
        $insertDataVal = "";
        foreach ($insertData as $insertkey => $insertVal){
            
            if (!$insertDataKey){
                $insertDataKey = array_keys($insertVal);
            }
            foreach ($insertVal as &$val){
                $val = "'".$val."'";
            }
            
            if ($insertkey == count($insertData)-1){
                $insertDataVal .= '('.implode(',', $insertVal).')';
            }else{
                $insertDataVal .= '('.implode(',', $insertVal).'),';
            }
            
        }
        
        $insertKey = implode(',', $insertDataKey);
        
        $statement = "insert into {$table}({$insertKey}) values {$insertDataVal}";
        
        return $statement;
        
    }
    
    public function SqlErr(){
        
        return [
            'code' => self::$pdo->errorCode(),
            'info' => self::$pdo->errorInfo()
        ];
    }
   
}


